﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FotoAnotSharp.Anotator;
using FotoAnotSharp.Gate;

namespace FotoAnotSharp
{
    public partial class CompositionView : UserControl
    {
        private Font mSelectionFont;

        public CompositionView()
        {
            InitializeComponent();

            mSelectionFont = new Font(Font, FontStyle.Underline);

            treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

            treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
            treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
        }

        void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            e.DrawDefault = true;
            if (e.State == TreeNodeStates.Selected)
            {
                e.DrawDefault = false;
                using (Brush br = new SolidBrush(treeView1.BackColor))
                    e.Graphics.FillRectangle(br, e.Bounds);
                e.Graphics.DrawString(e.Node.Text, mSelectionFont, Brushes.Black, e.Bounds.Left, e.Bounds.Top);
            }
        }

        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            OnSelectedPolygon(e.Node.Tag as AnotPolygon);
        }

        public delegate void PolygonSelectedDelegate(CompositionView sender, AnotPolygon selected);
        public event PolygonSelectedDelegate PolygonSelected;

        public AnotPolygon Selected
        {
            get { return treeView1.SelectedNode == null ? null : treeView1.SelectedNode.Tag as AnotPolygon; }
            set
            {
                if (value != null && mNodes.ContainsKey(value))
                    treeView1.SelectedNode = mNodes[value];
                else
                    treeView1.SelectedNode = null;
            }
        }

        private void OnSelectedPolygon(AnotPolygon select)
        {            
            if (PolygonSelected != null)
                PolygonSelected(this, select);
        }

        public void Clear()
        {
            mNodes.Clear();
            treeView1.Nodes.Clear();
        }

        public Func<AnotPolygon, string> GetObjectLabel { get; set; }

        private Dictionary<AnotPolygon, TreeNode> mNodes = new Dictionary<AnotPolygon, TreeNode>();
        public void LoadTree(IEnumerable<AnotPolygonTree> trees)
        {
            try
            {
                treeView1.BeginUpdate();

                Clear();

                foreach (AnotPolygonTree tree in trees)
                    AddTree(tree);

                treeView1.ExpandAll();
            }
            finally
            {
                treeView1.EndUpdate();
            }
        }

        public void ReloadLabels()
        {
            try
            {
                treeView1.BeginUpdate();

                foreach (KeyValuePair<AnotPolygon, TreeNode> pair in mNodes)
                {
                    pair.Value.Text = GetObjectLabel(pair.Key);
                }
            }
            finally
            {
                treeView1.EndUpdate();
            }
        }

        void AddTree(AnotPolygonTree tree, TreeNode node = null)
        {
            TreeNode n;
            if (node == null)
                n = treeView1.Nodes.Add(GetObjectLabel(tree.Polygon));
            else
                n = node.Nodes.Add(GetObjectLabel(tree.Polygon));

            n.Tag = tree.Polygon;
            mNodes[tree.Polygon] = n;

            foreach (AnotPolygonTree tr in tree.Nodes)
                AddTree(tr, n);
        }
    }
}
